/*     */ package com.zimbra.cs.fb;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Appointment;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.mail.ToXML;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class FreeBusyQuery
/*     */ {
/*     */   public static final int CALENDAR_FOLDER_ALL = -1;
/*     */   private Account mRequestor;
/*     */   private long mStart;
/*     */   private long mEnd;
/*     */   private String mExApptUid;
/*     */   private HashMap<String, Account> mTargets;
/*     */   private HashMap<String, Integer> mTargetFolder;
/*     */   private HttpServletRequest mReq;
/*     */   private ZimbraSoapContext mCtxt;
/*     */   
/*     */   public FreeBusyQuery(HttpServletRequest httpReq, ZimbraSoapContext zsc, Account requestor, long start, long end, String exApptUid)
/*     */   {
/*  56 */     this(httpReq, requestor, start, end, exApptUid);
/*  57 */     this.mCtxt = zsc;
/*     */   }
/*     */   
/*     */   public FreeBusyQuery(HttpServletRequest httpReq, Account requestor, long start, long end, String exApptUid) {
/*  61 */     this.mReq = httpReq;
/*  62 */     this.mRequestor = requestor;
/*  63 */     this.mStart = start;
/*  64 */     this.mEnd = end;
/*  65 */     this.mExApptUid = exApptUid;
/*  66 */     this.mTargets = new HashMap();
/*  67 */     this.mTargetFolder = new HashMap();
/*     */   }
/*     */   
/*     */   public void addAccountId(String accountId, int calFolderId) {
/*  71 */     addUser(accountId, getAccountFromId(accountId), calFolderId);
/*     */   }
/*     */   
/*     */   public void addEmailAddress(String emailAddr, int calFolderId) {
/*  75 */     addUser(emailAddr, getAccountFromName(emailAddr), calFolderId);
/*     */   }
/*     */   
/*     */   public void addId(String id, int calFolderId) {
/*  79 */     addUser(id, getAccountFromUid(id), calFolderId);
/*     */   }
/*     */   
/*     */   private void addUser(String id, Account acct, int calFolderId) {
/*  83 */     this.mTargets.put(id, acct);
/*  84 */     this.mTargetFolder.put(id, Integer.valueOf(calFolderId));
/*     */   }
/*     */   
/*     */   private Account getAccountFromUid(String uid) {
/*  88 */     Provisioning prov = Provisioning.getInstance();
/*  89 */     Account acct = null;
/*     */     try {
/*  91 */       if (Provisioning.isUUID(uid)) {
/*  92 */         acct = prov.get(Key.AccountBy.id, uid);
/*     */       } else
/*  94 */         acct = prov.get(Key.AccountBy.name, uid);
/*     */     } catch (ServiceException e) {
/*  96 */       acct = null;
/*     */     }
/*  98 */     return acct;
/*     */   }
/*     */   
/*     */   private Account getAccountFromId(String id) {
/* 102 */     try { return Provisioning.getInstance().get(Key.AccountBy.id, id);
/*     */     }
/*     */     catch (ServiceException e) {}
/* 105 */     return null;
/*     */   }
/*     */   
/*     */   private Account getAccountFromName(String name) {
/* 109 */     try { return Provisioning.getInstance().get(Key.AccountBy.name, name);
/*     */     }
/*     */     catch (ServiceException e) {}
/* 112 */     return null;
/*     */   }
/*     */   
/*     */   private void prepareRequests(ArrayList<FreeBusy> local, RemoteFreeBusyProvider remote, ArrayList<String> external) {
/* 116 */     for (String id : this.mTargets.keySet()) {
/* 117 */       Account acct = (Account)this.mTargets.get(id);
/*     */       try {
/* 119 */         if ((acct == null) || (acct.isAccountExternal()) || (acct.getBooleanAttr("zimbraFreebusyLocalMailboxNotActive", false)))
/*     */         {
/* 121 */           external.add(id);
/*     */         }
/*     */         else {
/* 124 */           int folder = ((Integer)this.mTargetFolder.get(id)).intValue();
/*     */           
/* 126 */           if (Provisioning.onLocalServer(acct)) {
/* 127 */             Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
/* 128 */             OperationContext octxt = null;
/* 129 */             if ((this.mCtxt != null) && (this.mCtxt.getAuthToken() != null)) {
/* 130 */               octxt = new OperationContext(this.mCtxt.getAuthToken());
/* 131 */             } else if (this.mRequestor != null) {
/* 132 */               octxt = new OperationContext(this.mRequestor);
/*     */             } else
/* 134 */               octxt = new OperationContext(GuestAccount.ANONYMOUS_ACCT);
/* 135 */             Appointment exAppt = null;
/* 136 */             if (this.mExApptUid != null) {
/* 137 */               CalendarItem ci = mbox.getCalendarItemByUid(octxt, this.mExApptUid);
/* 138 */               if ((ci instanceof Appointment))
/* 139 */                 exAppt = (Appointment)ci;
/*     */             }
/* 141 */             local.add(mbox.getFreeBusy(octxt, id, this.mStart, this.mEnd, folder, exAppt));
/*     */           } else {
/* 143 */             remote.addFreeBusyRequest(this.mRequestor, acct, id, this.mStart, this.mEnd, folder);
/*     */           }
/*     */         }
/* 146 */       } catch (ServiceException e) { ZimbraLog.fb.error("cannot get free/busy for " + id, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private int getHopcount() {
/* 152 */     int hopcount = -1;
/* 153 */     String hopcountStr = this.mReq.getHeader("X-ZIMBRA-FREEBUSY-HOPCOUNT");
/* 154 */     if (hopcountStr != null) {
/*     */       try {
/* 156 */         hopcount = Integer.parseInt(hopcountStr);
/*     */       }
/*     */       catch (NumberFormatException nfe) {}
/*     */     }
/*     */     
/* 161 */     return hopcount;
/*     */   }
/*     */   
/* 164 */   public Collection<FreeBusy> getResults() { RemoteFreeBusyProvider remote = new RemoteFreeBusyProvider(this.mReq, this.mCtxt, this.mStart, this.mEnd, this.mExApptUid);
/* 165 */     ArrayList<String> external = new ArrayList();
/* 166 */     ArrayList<FreeBusy> fbList = new ArrayList();
/* 167 */     prepareRequests(fbList, remote, external);
/*     */     
/* 169 */     fbList.addAll(remote.getResults());
/* 170 */     if (external.size() > 0)
/* 171 */       fbList.addAll(FreeBusyProvider.getRemoteFreeBusy(this.mRequestor, external, this.mStart, this.mEnd, -1, getHopcount()));
/* 172 */     return fbList;
/*     */   }
/*     */   
/*     */   public void getResults(Element response) {
/* 176 */     RemoteFreeBusyProvider remote = new RemoteFreeBusyProvider(this.mReq, this.mCtxt, this.mStart, this.mEnd, this.mExApptUid);
/* 177 */     ArrayList<String> external = new ArrayList();
/* 178 */     ArrayList<FreeBusy> fbList = new ArrayList();
/* 179 */     prepareRequests(fbList, remote, external);
/*     */     
/* 181 */     for (FreeBusy fb : fbList)
/* 182 */       ToXML.encodeFreeBusy(response, fb);
/* 183 */     remote.addResults(response);
/* 184 */     if (external.size() > 0) {
/* 185 */       FreeBusyProvider.getRemoteFreeBusy(this.mRequestor, response, external, this.mStart, this.mEnd, -1, getHopcount());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/fb/FreeBusyQuery.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */